/**
 * Created by zhaojm on 15/3/17.
 */

frame.BaseHubLayer = cc.Layer.extend({

    minSwipdistance : 100,
    minSwiptime : 1000,    //毫秒
    maxClickedDis : 20,

    E_SWIP_DIR : {
        E_INVAILD : 0,
        E_LEFT : 1,
        E_RIGHT : 2,
        E_UP : 3,
        E_DOWN : 4
    },
    isTouch : false,

    isMoved : false,

    pressTimes : 0,

    touchCounts : 0,

    m_longProgress : false,
    m_startPoint : null,
    m_endPoint : null,
    m_startTime : null,


    maxTouchCount:4,    // 支持的 最大连续点击数
    _touchListener:null,



    ctor:function(){
        this._super();
        //cc.eventManager.addListener({
        //    event: cc.EventListener.TOUCH_ONE_BY_ONE,
        //    swallowTouches: true,           // true 为不向下传递
        //    onTouchBegan: this.onTouchBegan,
        //    onTouchMoved: this.onTouchMoved,
        //    onTouchEnded: this.onTouchEnded,
        //    onTouchCancel: this.onTouchCancel
        //}, this);

        this.setTouchEnabled(true);

        this.setMaxTouchCount(4);

        that_in_base_touch_layer = this;

    },

    setTouchEnabled:function (e) {
        if(this._touchListener)
            cc.eventManager.removeListener(this._touchListener);
        this._touchListener = null;
        if (!e) {
        } else {
            var listener = cc.EventListener.create({
                event: cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: true   ,       // true 为不向下传递
                onTouchBegan: this.onTouchBegan,
                onTouchMoved: this.onTouchMoved,
                onTouchEnded: this.onTouchEnded,
                onTouchCancel: this.onTouchCancel
            });
            this._touchListener = listener;
            cc.eventManager.addListener(listener, this);
        }
    },



    // -----------------------------------------------------------------------------------------------------
    updateSingleDelay : function(ft){
        var self = that_in_base_touch_layer;
        if (self.touchCounts == 1) {
            self.onSingleClick(self.m_endPoint);
            self.touchCounts=0;
        }

    },

    updateDoubleDelay : function(ft) {
        var self = that_in_base_touch_layer;
        if (self.touchCounts == 2 ) {
            self.onDoubleClick(self.m_endPoint);
            self.touchCounts=0;
        }
    },

    updateThreeDelay : function(ft) {
        var self = that_in_base_touch_layer;
        if (self.touchCounts == 3 ) {
            self.onThreeClick(self.m_endPoint);
            self.touchCounts=0;
        }
    },

    updatelongprogress : function(ft) {
        var self = that_in_base_touch_layer;
        if (self.isTouch) {
            self.pressTimes++;

            if (self.pressTimes >= 2) {
                self.m_longProgress=true;
                self.onLongPressed(self.m_endPoint);

            }
        }else {
            self.pressTimes=0;
        }
    },

    getCurrentTime : function() {
        //var tm;
        //gettimeofday(tm, null);
        var myDate = new Date();
        //myDate.getSeconds();     //获取当前秒数(0-59)
        //myDate.getMilliseconds();    //获取当前毫秒数(0-999)

        //return tm.tv_sec * 1000 + tm.tv_usec / 1000;
        return myDate.getSeconds() + myDate.getMilliseconds() / 1000;
    },

    setMaxTouchCount : function(num){
        this.maxTouchCount = num;
    },




    onTouchBegan : function(touch, event){
        var self = that_in_base_touch_layer;
        self.m_startPoint=touch.getLocation();

        self.isTouch=true;

        self.m_startTime=self.getCurrentTime();

        //处理长按事件
        self.schedule(self.updatelongprogress,1);

        return true;
    },

    onTouchMoved : function(touch, event){
        var self = that_in_base_touch_layer;
        self.isMoved=true;
        var curPoint=touch.getLocation();
        var prePoint = touch.getPreviousLocation();
        self.onMove(prePoint, curPoint);
    },

    onTouchEnded : function(touch, event){
        var self = that_in_base_touch_layer;
        self.isTouch=false;
        self.pressTimes=0;
        self.unschedule(self.updatelongprogress);

        //如果刚完成长按事件 则把按下次数清零 长按状态置空 直接返回 不继续执行
        if (self.m_longProgress ) {
            self.touchCounts=0;
            self.m_longProgress=false;

            return;
        }

        self.m_endPoint=touch.getLocation();

        var endTime=self.getCurrentTime();

        var timeDis=endTime - self.m_startTime;

        var dir = self.GetSwipDir(self.m_startPoint, self.m_endPoint,timeDis);

        if ( dir != self.E_SWIP_DIR.E_INVAILD) {
            self.onSwip(self.m_startPoint, self.m_endPoint, dir);
            return;
        }


        if (self.isMoved) {
            self.isMoved=false;
            return;
        }

        if (self.touchCounts == 3){
            if(self.maxTouchCount == 4){
                self.onFourthClick(self.m_endPoint);
                self.touchCounts=0;
            }else if(self.maxTouchCount < 4){

            }else if(self.maxTouchCount > 4){

            }
        }
        else if (self.touchCounts == 2) {
            if(self.maxTouchCount == 3){
                self.onThreeClick(self.m_endPoint);
                self.touchCounts=0;
            }else if(self.maxTouchCount < 3){

            }else if(self.maxTouchCount > 3){
                self.scheduleOnce(self.updateThreeDelay, 0.25);
                self.touchCounts++;
            }
        }
        else if (self.touchCounts == 1) {
            if(self.maxTouchCount == 2){
                self.onDoubleClick(self.m_endPoint);
                self.touchCounts = 0;
            }else if(self.maxTouchCount < 2){

            }else if(self.maxTouchCount > 2){
                self.scheduleOnce(self.updateDoubleDelay, 0.25);
                self.touchCounts++;
            }
        }
        else if (self.touchCounts == 0) {
            if(self.maxTouchCount == 1){
                self.onSingleClick(self.m_endPoint);
                self.touchCounts = 0;
            }else if(self.maxTouchCount < 1){

            }else if(self.maxTouchCount > 1){
                self.scheduleOnce(self.updateSingleDelay, 0.25);
                self.touchCounts++;
            }
        }





    },
    onTouchCancel : function(touch, event){
        var self = that_in_base_touch_layer;
        self.onTouchEnded(touch, event);
    },

    GetSwipDir : function(startPos, endPos, timeDis) {
        var self = that_in_base_touch_layer;
        if (timeDis < self.minSwiptime) {
            return self.E_SWIP_DIR.E_INVAILD;
        }

        if (abs(endPos.x - startPos.x) > self.minSwipdistance) {
            if (endPos.x > startPos.x) {
                return self.E_SWIP_DIR.E_RIGHT;
            }else{
                return self.E_SWIP_DIR.E_LEFT;
            }
        }
        if (abs(endPos.y - startPos.y) > self.minSwipdistance) {
            if (endPos.y > startPos.y) {
                return self.E_SWIP_DIR.E_UP;
            }else{
                return self.E_SWIP_DIR.E_DOWN;
            }
        }
        return self.E_SWIP_DIR.E_INVAILD;
    },


    //------------------------------------------------------------------------------
    // 需要重载的函数

    onSingleClick : function(pos){},        //单击
    onDoubleClick : function(pos){},        //双击
    onThreeClick : function(pos){},         //3连击
    onFourthClick : function(pos){},        // 4连击
    onLongPressed : function(pos){},        //长按
    onMove : function(prePoint, curPoint){},          //移动
    onSwip : function(startPos, endPos, dir){},   //滑动



});

